1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.base;
18
19 import static com.google.common.base.Preconditions.checkNotNull;
20
21 import com.google.common.annotations.GwtCompatible;
22
23 import java.util.Collections;
24 import java.util.Set;
25
26 import javax.annotation.Nullable;
27
28
29
30
31 @GwtCompatible
32 final class Present<T> extends Optional<T> {
33 private final T reference;
34
35 Present(T reference) {
36 this.reference = reference;
37 }
38
39 @Override public boolean isPresent() {
40 return true;
41 }
42
43 @Override public T get() {
44 return reference;
45 }
46
47 @Override public T or(T defaultValue) {
48 checkNotNull(defaultValue, "use Optional.orNull() instead of Optional.or(null)");
49 return reference;
50 }
51
52 @Override public Optional<T> or(Optional<? extends T> secondChoice) {
53 checkNotNull(secondChoice);
54 return this;
55 }
56
57 @Override public T or(Supplier<? extends T> supplier) {
58 checkNotNull(supplier);
59 return reference;
60 }
61
62 @Override public T orNull() {
63 return reference;
64 }
65
66 @Override public Set<T> asSet() {
67 return Collections.singleton(reference);
68 }
69
70 @Override public <V> Optional<V> transform(Function<? super T, V> function) {
71 return new Present<V>(checkNotNull(function.apply(reference),
72 "the Function passed to Optional.transform() must not return null."));
73 }
74
75 @Override public boolean equals(@Nullable Object object) {
76 if (object instanceof Present) {
77 Present<?> other = (Present<?>) object;
78 return reference.equals(other.reference);
79 }
80 return false;
81 }
82
83 @Override public int hashCode() {
84 return 0x598df91c + reference.hashCode();
85 }
86
87 @Override public String toString() {
88 return "Optional.of(" + reference + ")";
89 }
90
91 private static final long serialVersionUID = 0;
92 }